﻿using UnityEditor;
using UnityEngine;

public class FormatAnimSpriteName
{
    [MenuItem("Assets/Mini Tools/Texture/规范Anim Sprite名字", false, 413)]
    private static void Main()
    {
        EditorUtils.ForeachTexture(ProcessTexture);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        Debug.Log("处理完成");
    }

    private static readonly string[] Dir = {
        "left","right","up","down"
    };

    private static void ProcessTexture(TextureImporter importer, string path)
    {
        Debug.Log(path);

        var spritesheet = importer.spritesheet;
        //var name = path[(path.LastIndexOf('/') + 1)..path.LastIndexOf('.')];
        var name = path[(path.LastIndexOf('_') + 1)..path.LastIndexOf('.')];
        for (int i = 0; i < spritesheet.Length; i++)
        {
            var item = spritesheet[i];
            var x = (int)(item.rect.x / item.rect.width);
            var y = (int)(item.rect.y / item.rect.height);

            spritesheet[i].name = $"{name}_{Dir[y]}_{x}";
        }
        importer.spritesheet = spritesheet;

        EditorUtility.SetDirty(importer);
        importer.SaveAndReimport();
    }
}
